package com.service.frontdesk;

import com.bean.frontdesk.bo.AddMsgBO;
import com.bean.frontdesk.vo.*;
import com.bean.pojo.Good;
import com.bean.pojo.GoodsType;
import com.bean.pojo.Space;
import com.dao.frontdest.GoodsMapper;
import com.dao.frontdest.UserMapper;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-21 16:39
 **/
public class GoodServiceImplement implements GoodService{
    @Override
    public List<GoodsType> showAllType() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsType> goodsTypes = mapper.selectAllTypes();
        sqlSession.close();
        return goodsTypes;
    }

    @Override
    public List<ShowGoodsVO> showAllGoodsByTypeId(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<ShowGoodsVO> showGoodsVOS = mapper.selectAllGoodsByTypeId(id);
        sqlSession.close();
        return showGoodsVOS;
    }

    @Override
    public List<SelectVO> searchGoodsLikeName(String name) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<SelectVO> selectVOS = mapper.selectLikeName(name);
        return selectVOS;
    }

    @Override
    public GoodsDetailVO showGoodsDetails(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        Good good = mapper.selectGoodById(id);
        List<Space> spaces = mapper.selectAllSpaceByGoodId(id);
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO(good.getImg(),good.getName(),good.getDetails(),good.getTypeId(),spaces);
        return goodsDetailVO;
    }

    @Override
    public List<MsgVO> showMsg(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<MsgVO> msgVOS = mapper.showMsg(id);
        return msgVOS;
    }

    @Override
    public int addMsgByAddMsgBO(AddMsgBO addMsgBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        int i = mapper1.selectIdByNickName(addMsgBO.getToken());
        addMsgBO.setUserId(i);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        String createTime = sf.format(date);
        addMsgBO.setAddtime(createTime);
        int insertState = mapper.insertByAddMsgBO(addMsgBO);
        if (insertState!=0){
            return 200;
        }

        return 404;
    }

    @Override
    public GoodCommentVO showGoodComment(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<InGoodCommentVO> inGoodCommentVOS = mapper.selectGoodCommentList(id);
        String v = mapper.selectAvg(id);
        if (v==null){
            GoodCommentVO goodCommentVO = new GoodCommentVO();
            goodCommentVO.setCommentList(inGoodCommentVOS);
            return goodCommentVO;
        }
        GoodCommentVO goodCommentVO = new GoodCommentVO(inGoodCommentVOS,Double.parseDouble(v));
        return goodCommentVO;
    }
}
